package com.yiyi.credit.manage.abutment.manage.common.handler;



import com.yiyi.credit.manage.abutment.common.bean.Response;
import com.yiyi.credit.manage.abutment.common.enums.ResponseCode;
import com.yiyi.credit.manage.abutment.common.exceptions.FatalException;
import com.yiyi.credit.manage.abutment.common.exceptions.ServiceException;
import com.yiyi.credit.manage.abutment.common.mail.MailService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.MissingServletRequestParameterException;
import org.springframework.web.bind.UnsatisfiedServletRequestParameterException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.text.MessageFormat;


@ResponseBody
@ControllerAdvice
class GlobalExceptionHandler {

    private final Logger log = LoggerFactory.getLogger(GlobalExceptionHandler.class);

    @Autowired
    private MailService mailService;

    @ExceptionHandler(FatalException.class)
    public Response fatalExceptionHandle(FatalException ex) {
        log.error("严重异常：{}", ex.getMessage());
        return new Response<>(ResponseCode.FATAL_ERROR);
    }

    @ExceptionHandler(MissingServletRequestParameterException.class)
    public Object handleMissingServletRequestParameterException(MissingServletRequestParameterException ex,
                                                                HttpServletRequest request) {

        writeLog(ex, request);
        return new Response<>(ResponseCode.PARAM_ERR);
    }

    @ExceptionHandler(UnsatisfiedServletRequestParameterException.class)
    public Object handleRequestParameterException(UnsatisfiedServletRequestParameterException exception) {
        Response<Object> cp = new Response<Object>();
        cp.setCode("50501");
        cp.setMessage("参数错误!" + exception.getMessage());
        return cp;
    }

    @ExceptionHandler(Exception.class)
    public Object handleException(Exception ex, HttpServletRequest request, HttpServletResponse response) {

        writeLog(ex, request);
        return new Response<>(ResponseCode.SERVICE_ERR);
    }

    @ExceptionHandler(ServiceException.class)
    public Object serviceExceptionHandle(ServiceException ex, HttpServletRequest request,
                                         HttpServletResponse response) {

        Response<Object> cp = new Response<>();
        cp.setCode(ex.getErrorCode());
        cp.setMessage(ex.getMessage());
        writeLog(ex, request);
        return cp;
    }

    private void writeLog(Exception ex, HttpServletRequest request) {
        String url = MessageFormat.format("Exception :{0}?{1}", request.getRequestURL(), request.getQueryString());
        log.error(url, ex);
    }
}
